<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import s94Mjs from "s94-js";
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {decode_config} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let type_map = {
      "click": "点击事件",
      "view": "网页",
      "miniprogram": "小程序",
      "scancode_push": "扫码推事件",
      "scancode_waitmsg": "扫码待回复",
      "pic_sysphoto": "拍照发图",
      "pic_photo_or_album": "拍照或相册发图",
      "pic_weixin": "微信相册发图",
      "location_select": "发送位置",
      "article_id": "图文消息",
      "article_view_limited": "打开图文"
    };
    let forms_config = {
      name: {label:'名称',name:'name',type:'text_one',tips:'菜单按钮标题'},
      parent_id: {label:'父级按钮',name:'parent_id',type:'select_one',config:[],tips:'',value: '0'},
      type: {label:'按钮类型',name:'type',type:'select_one',config:type_map,tips:''},

      media_id: {label:'素材id',name:'media_id',type:'text_one',tips:'调用新增永久素材接口返回的合法media_id', remove:true},
      article_id: {label:'图文id',name:'article_id',type:'text_one',tips:'图文素材发布后获得的合法 article_id', remove:true},
      appid: {label:'appid',name:'appid',type:'text_one',tips:'小程序的appid（仅认证公众号可配置）', remove:true},
      pagepath: {label:'页面路径',name:'pagepath',type:'text_one',tips:'小程序的页面路径', remove:true},
      url: {label:'url',name:'url',type:'text_one',tips:'网页 链接，例如：https://www.taiyusaas.com/', remove:true},
      key: {label:'key',name:'key',type:'text_one',tips:'菜单KEY值，用于消息接口推送', remove:true},

      sort: {label:'排序',name:'sort',type:'text_one',tips:'排序，从大到小',value:50},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/wechat_menu/edit?id=`+this.id : '/admin/wechat_menu/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('wechat_menu/index', '$refs.list_url.list');
          iframe.hide('wechat_menu/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    }
  },
  watch:{
    'config.type.value'(v){
      var keys = [], app = this;
      switch (v) {
        case 'media_id': case 'view_limited': keys.push('media_id'); break;
        case 'article_id': case 'article_view_limited': keys.push('article_id'); break;
        case 'miniprogram': {
          keys.push('appid');
          keys.push('pagepath');
        }
        case 'view': keys.push('url'); break;
        default: keys.push('key'); break;
      }
      ['media_id','article_id','appid','pagepath','url','key'].forEach(function (k){
        app.config[k].remove = !~keys.indexOf(k);
      })
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/wechat_menu/info?id='+this.id, cache:true}, (res)=>{
        res.data = s94Mjs.merge(res.data, JSON.parse(res.data.extend));
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('wechat_menu/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    //其他初始化操作
    ajax({url:'/admin/wechat_menu/list?where[parent_id][eq]=0',cache:true}, (res)=>{
      let parent_id_config = decode_config(res.data.list, {content_key:'name', value_key:'id', parent_key:'parent_id'});
      parent_id_config.unshift({content:'顶级菜单', value:'0'});
      app.config.parent_id.config = parent_id_config;
      return false;
    })
  }
}
</script>
